﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Plugin.Default.SwedenGrid
{
    /// <summary>
    /// Interaktionslogik für SwedenGridCellInfoWindow.xaml
    /// </summary>
    public partial class SwedenGridCellInfoWindow : Window
    {
        public static SwedenGridCellInfo ShowDialog(SwedenGridCellInfo info)
        {
            SwedenGridCellInfoWindow win = new SwedenGridCellInfoWindow();

            if (info != null)
            {
                switch (info.CellType)
                {
                    case SwedenGridCellType.Character:
                        win.rbCharacter.IsChecked = true;
                        win.txtCharacter.Text = info.CellContent.Text;
                        win.ckbShowAlways.IsChecked = info.ShowAlways;
                        break;
                    case SwedenGridCellType.Question:
                        win.rbQuestion.IsChecked = true;
                        win.txtText.Text = info.CellContent.Text;
                        win.cmbDirection.SelectedItem = info.CellContent.CellDirection;
                        if (info.AdditionalCellContent != null)
                        {
                            win.ckbAdditional.IsChecked = true;
                            win.txtAdditionalText.Text = info.AdditionalCellContent.Text;
                            win.cmbAdditionalDirection.SelectedItem = info.AdditionalCellContent.CellDirection;
                        }
                        break;
                    default:
                        throw new NotImplementedException(info.CellType.ToString());
                }
            }

            if (win.ShowDialog().GetValueOrDefault(false))
            {
                SwedenGridCellInfo result = new SwedenGridCellInfo();
                if (win.rbCharacter.IsChecked.GetValueOrDefault(false))
                {
                    result.CellType = SwedenGridCellType.Character;
                    result.CellContent.Text = win.txtCharacter.Text;
                    result.ShowAlways = win.ckbShowAlways.IsChecked.GetValueOrDefault(false);
                }
                else if (win.rbQuestion.IsChecked.GetValueOrDefault(false))
                {
                    result.CellType = SwedenGridCellType.Question;
                    result.CellContent.Text = win.txtText.Text;
                    result.CellContent.CellDirection = (SwedenGridCellDirection) win.cmbDirection.SelectedItem;
                    if (win.ckbAdditional.IsChecked.GetValueOrDefault(false))
                    {
                        result.AdditionalCellContent = new SwedenGridCellContent
                            {
                                Text = win.txtAdditionalText.Text,
                                CellDirection = (SwedenGridCellDirection) win.cmbAdditionalDirection.SelectedItem
                            };
                    }
                }
                else
                    throw new NotImplementedException();

                return result;
            }
            else
            {
                return null;
            }
        }

        private SwedenGridCellInfoWindow()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        /// <summary>
        /// Used in XAML
        /// </summary>
        private void CheckDirection()
        {
            #region Check for same item direction

            bool failed = rbQuestion.IsChecked.GetValueOrDefault(false) &&
                          ckbAdditional.IsChecked.GetValueOrDefault(false) &&
                          cmbDirection.SelectedItem.Equals(cmbAdditionalDirection.SelectedItem);

            btnOK.IsEnabled = !failed;
            if (failed)
            {
                txtError.Text = "Direction Error: Both question fields go in the same direction!";
                txtError.Visibility = Visibility.Visible;
                return;
            }

            #endregion

            #region Check for wrong item direction

            failed = cmbDirection.SelectedItem == (object) SwedenGridCellDirection.Bottom ||
                     cmbAdditionalDirection.SelectedItem == (object) SwedenGridCellDirection.Top;

            btnOK.IsEnabled = !failed;
            if (failed)
            {
                txtError.Text = "Direction Problem: First item shows to bottom or second item shows to top!";
                txtError.Visibility = Visibility.Visible;
                return;
            }

            #endregion

            txtError.Visibility = Visibility.Hidden;
        }
    }
}
